//
//  TCLYAllGoodsViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/9/5.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//
/***************************************************************************************所有积分商品的界面******************************************/
#import "TCLYAllGoodsViewController.h"
#import "Macros.h"
#import "Masonry.h"
#import "TCLYHomeModel.h"
#import "TCLYIntegralGoodsCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "TCLYIntegralGoodsViewController.h"

@interface TCLYAllGoodsViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong)TCLYHomeModel * homeModel;
@property (nonatomic, strong)UICollectionView * allGoodsCollectionView;

@end

@implementation TCLYAllGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBarHidden = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadIntegralAllGoods) name:@"loadIntegralGoods" object:nil];
    self.homeModel = [[TCLYHomeModel alloc] init];
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.parameterDic[@"cat_id"], @"cat_id", nil];
    [self.homeModel loadIntegralAllGoodsParameter:dic];
    
    [self createView];
    [self createTitleView];
}
-(void)loadIntegralAllGoods{
    [self.allGoodsCollectionView reloadData];
}
-(void)createTitleView{
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = self.parameterDic[@"cat_name"];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *)sender).tag) {
        case 91:{
            [self.navigationController popViewControllerAnimated:YES];
            self.navigationController.navigationBarHidden = YES;
            self.tabBarController.tabBar.hidden = YES;
        }
            break;
        case 92:{
            
        }
            break;
    }
}

-(void) createView{
    UICollectionViewFlowLayout * layout1 = [[UICollectionViewFlowLayout alloc]init];
    layout1.itemSize = CGSizeMake((screenWidth-12)/2,200/667.0*screenHeight);
    layout1.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout1.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.allGoodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight) collectionViewLayout:layout1];
    self.allGoodsCollectionView.backgroundColor = [UIColor whiteColor];
    [self.allGoodsCollectionView registerClass:[TCLYIntegralGoodsCollectionViewCell class] forCellWithReuseIdentifier:@"integralGoodsCell"];
    self.allGoodsCollectionView.delegate = self;
    self.allGoodsCollectionView.dataSource = self;
    [self.view addSubview:self.allGoodsCollectionView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.homeModel.integralGoodsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"integralGoodsCell";
    TCLYIntegralGoodsCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@%@",self.homeModel.integralGoodsArr[indexPath.row][@"brand"],self.homeModel.integralGoodsArr[indexPath.row][@"goods_name"]];
    cell.introduceLabel.text = self.homeModel.integralGoodsArr[indexPath.row][@"standard"];
    cell.priceLabel.text = [NSString stringWithFormat:@"%@",self.homeModel.integralGoodsArr[indexPath.row][@"integral_val"]];
    cell.numLabel.text = [NSString stringWithFormat:@"%@人兑换",self.homeModel.integralGoodsArr[indexPath.row][@"exc_num"]];
    NSURL * url = [NSURL URLWithString:self.homeModel.integralGoodsArr[indexPath.row][@"goods_pic"]];
    [cell.goodsImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"]];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TCLYIntegralGoodsViewController * integralGoodsViewCtr = [[TCLYIntegralGoodsViewController alloc] init];
    integralGoodsViewCtr.parameterDic = self.homeModel.integralGoodsArr[indexPath.row];
    integralGoodsViewCtr.isIntegralHome = NO;
    [self.navigationController pushViewController:integralGoodsViewCtr animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
